package com.mm.fam.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mm.fam.web.base.ResponseModel;
import com.mm.fam.web.base.ResponseUtil;
import com.mm.fam.web.entity.SysOrganization;
import com.mm.fam.web.mapper.SysOrganizationMapper;
import com.mm.fam.web.service.SysOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author MaLiZhi
 * @date 2019年04月22日 09:41
 */
@Service
public class SysOrganizationServiceImpl extends ServiceImpl<SysOrganizationMapper, SysOrganization> implements SysOrganizationService {
    @Autowired
    private SysOrganizationMapper sysOrganizationMapper;

    @Override
    public String saveOrUpdataSysOrganization(SysOrganization sysOrganization) {
        Date data = new Date();
        try {
            sysOrganization.setCreateDate(data);
            if (sysOrganization.getId() == null) {
                // 获取当前机构号最大值
                int maxOrgNum = sysOrganizationMapper.getMaxOrnNum();
                // 新增时,生成当前机构号最大值+1
                sysOrganization.setOrganizationNum(maxOrgNum + 1);
                int result = sysOrganizationMapper.saveSysOrganization(sysOrganization);
                if (result > 0) {
                    return "操作成功";
                } else {
                    return "操作失败";
                }
            } else {
                int upResult = sysOrganizationMapper.updateSysOrganization(sysOrganization);
                if (upResult > 0) {
                    return "操作成功";
                } else {
                    return "操作失败";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public IPage<SysOrganization> getAllSysOrganization(String OrganizationNum, String OrganizationName, Integer pageNum, Integer pageSize) {
        Page<SysOrganization> page = new Page(pageNum, pageSize);
        List<SysOrganization> list = sysOrganizationMapper.getAllSysOrganization(page, OrganizationNum, OrganizationName);
        page.setRecords(list);
        return page;
    }

    @Override
    public Integer delOrgById(String id) {
        return sysOrganizationMapper.delOrgById(id);
    }

    @Override
    public List<SysOrganization> getSysOrganizationById(String id) {
        List<SysOrganization> orgList = sysOrganizationMapper.selectIInsurePeriodById(id);
        return orgList;
    }

    @Override
    public List<SysOrganization> getParent() {
        List<SysOrganization> parentList = sysOrganizationMapper.getParent();
        return parentList;
    }

    @Override
    public Integer getMaxOrnNum() {
        Integer maxNum = sysOrganizationMapper.getMaxOrnNum();
        return maxNum;
    }

    @Override
    public ResponseEntity<ResponseModel<List<SysOrganization>>> queryOrganizationList() {
        ResponseEntity<ResponseModel<List<SysOrganization>>> re = null;
        List<SysOrganization> sysOrganizations = sysOrganizationMapper.selectList(null);
        re = ResponseUtil.success(sysOrganizations);
        return re;
    }
}
